package com.xyht.sca_s.student_manage_system.modules.advanceRegistration.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.modules.advanceRegistration.entity.RxAdvanceRegistrationMiddleSchool;
import com.xyht.sca_s.student_manage_system.modules.advanceRegistration.entity.RxAdvanceRegistrationMiddleSchoolClasses;
import com.xyht.sca_s.student_manage_system.modules.advanceRegistration.entity.req.RxAdvanceRegistrationMiddleSchoolClassesDeletedReq;
import com.xyht.sca_s.student_manage_system.modules.advanceRegistration.entity.req.RxAdvanceRegistrationMiddleSchoolClassesUpdateReq;
import com.xyht.sca_s.student_manage_system.modules.advanceRegistration.entity.req.RxAdvanceRegistrationMiddleSchoolReq;
import com.xyht.sca_s.student_manage_system.modules.advanceRegistration.entity.resp.RxAdvanceRegistrationMiddleSchoolClassesResp;
import com.xyht.sca_s.student_manage_system.modules.advanceRegistration.entity.resp.RxAdvanceRegistrationMiddleSchoolResp;
import com.xyht.sca_s.student_manage_system.modules.advanceRegistration.mapper.RxAdvanceRegistrationMiddleSchoolClassesMapper;
import com.xyht.sca_s.student_manage_system.modules.advanceRegistration.mapper.RxAdvanceRegistrationMiddleSchoolMapper;
import com.xyht.sca_s.student_manage_system.modules.advanceRegistration.service.RxAdvanceRegistrationMiddleSchoolService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;

/**
 * <p>
 * 招生预报名初中学校 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2024-04-12
 */
@Service
public class RxAdvanceRegistrationMiddleSchoolServiceImpl extends ServiceImpl<RxAdvanceRegistrationMiddleSchoolMapper, RxAdvanceRegistrationMiddleSchool> implements RxAdvanceRegistrationMiddleSchoolService {


    @Resource
    private RxAdvanceRegistrationMiddleSchoolMapper rxAdvanceRegistrationMiddleSchoolMapper;
    @Resource
    private RxAdvanceRegistrationMiddleSchoolClassesMapper rxAdvanceRegistrationMiddleSchoolClassesMapper;

    @Override
    public ResponseResult addMiddleSchool(RxAdvanceRegistrationMiddleSchoolReq addReq, String userId) {
        if (isNullOrEmpty(addReq.getMiddleSchoolName())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        List<RxAdvanceRegistrationMiddleSchool> middleSchoolList = rxAdvanceRegistrationMiddleSchoolMapper.selectList(new LambdaQueryWrapper<RxAdvanceRegistrationMiddleSchool>()
                .eq(RxAdvanceRegistrationMiddleSchool::getMiddleSchoolName, addReq.getMiddleSchoolName()));
        if (!middleSchoolList.isEmpty()) {
            return CommonResult.failed(CommonCodeEnum.ADVANCE_REGISTRATION_MIDDLE_SCHOOL_DUPLICATE);
        }
        //创建初中学校
        RxAdvanceRegistrationMiddleSchool rxAdvanceRegistrationMiddleSchool = new RxAdvanceRegistrationMiddleSchool();
        BeanUtils.copyProperties(addReq, rxAdvanceRegistrationMiddleSchool);
        rxAdvanceRegistrationMiddleSchool.setUserId(userId);
        rxAdvanceRegistrationMiddleSchoolMapper.insert(rxAdvanceRegistrationMiddleSchool);

        //创建该学校的班级
//        for (int i = 1; i < classNum + 1; i++) {
//            RxAdvanceRegistrationMiddleSchoolClasses rxAdvanceRegistrationMiddleSchoolClasses = new RxAdvanceRegistrationMiddleSchoolClasses();
//            rxAdvanceRegistrationMiddleSchoolClasses.setMiddleSchoolId(middleSchoolId);
//            rxAdvanceRegistrationMiddleSchoolClasses.setMiddleSchoolName(rxAdvanceRegistrationMiddleSchool.getMiddleSchoolName());
//            rxAdvanceRegistrationMiddleSchoolClasses.setMiddleSchoolClassesName(i + "班级");
//            rxAdvanceRegistrationMiddleSchoolClasses.setUserId(userId);
//            rxAdvanceRegistrationMiddleSchoolClasses.setRemark(i);
//            rxAdvanceRegistrationMiddleSchoolClassesMapper.insert(rxAdvanceRegistrationMiddleSchoolClasses);
//        }
        return CommonResult.success();
    }

    @Override
    public ResponseResult deletedMiddleSchool(RxAdvanceRegistrationMiddleSchoolReq deletedReq) {
        if (isNullOrEmpty(deletedReq.getId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        RxAdvanceRegistrationMiddleSchool rxAdvanceRegistrationMiddleSchool = rxAdvanceRegistrationMiddleSchoolMapper.selectById(deletedReq.getId());
        if (isNullOrEmpty(rxAdvanceRegistrationMiddleSchool)) {
            return CommonResult.failed(CommonCodeEnum.ADVANCE_REGISTRATION_MIDDLE_SCHOOL_NOT_EXIST);
        }

//        List<RxAdvanceRegistrationMiddleSchoolClasses> schoolClassesList = rxAdvanceRegistrationMiddleSchoolClassesMapper.selectList(new LambdaQueryWrapper<RxAdvanceRegistrationMiddleSchoolClasses>()
//                .eq(RxAdvanceRegistrationMiddleSchoolClasses::getMiddleSchoolId, deletedReq.getId()));
//        if (!schoolClassesList.isEmpty()) {
//            return CommonResult.failed(CommonCodeEnum.ADVANCE_REGISTRATION_MIDDLE_SCHOOL_EXIST_CLASSES);
//        }
//        //删除班级
//        rxAdvanceRegistrationMiddleSchoolClassesMapper.delete(new LambdaQueryWrapper<RxAdvanceRegistrationMiddleSchoolClasses>()
//                .eq(RxAdvanceRegistrationMiddleSchoolClasses::getMiddleSchoolId, deletedReq.getId()));
        //删除学校
        rxAdvanceRegistrationMiddleSchoolMapper.deleteById(deletedReq.getId());
        return CommonResult.success();
    }

    @Override
    public ResponseResult updateMiddleSchool(RxAdvanceRegistrationMiddleSchoolReq updateReq, String userId) {
        if (isNullOrEmpty(updateReq.getId(),updateReq.getMiddleSchoolName())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
//        Integer classNum = updateReq.getClassNum();
//        if (classNum < 1) {
//            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
//        }

        RxAdvanceRegistrationMiddleSchool rxAdvanceRegistrationMiddleSchool = rxAdvanceRegistrationMiddleSchoolMapper.selectById(updateReq.getId());
        if (isNullOrEmpty(rxAdvanceRegistrationMiddleSchool)) {
            return CommonResult.failed(CommonCodeEnum.ADVANCE_REGISTRATION_MIDDLE_SCHOOL_NOT_EXIST);
        }

        List<RxAdvanceRegistrationMiddleSchool> middleSchoolList = rxAdvanceRegistrationMiddleSchoolMapper.selectList(new LambdaQueryWrapper<RxAdvanceRegistrationMiddleSchool>()
                .ne(RxAdvanceRegistrationMiddleSchool::getId, updateReq.getId())
                .eq(RxAdvanceRegistrationMiddleSchool::getMiddleSchoolName, updateReq.getMiddleSchoolName()));
        if (!middleSchoolList.isEmpty()) {
            return CommonResult.failed(CommonCodeEnum.ADVANCE_REGISTRATION_MIDDLE_SCHOOL_DUPLICATE);
        }
        BeanUtils.copyProperties(updateReq, rxAdvanceRegistrationMiddleSchool);
        rxAdvanceRegistrationMiddleSchoolMapper.updateById(rxAdvanceRegistrationMiddleSchool);
        //学校下的班级总数
//        Integer classesCount = rxAdvanceRegistrationMiddleSchoolClassesMapper.selectCount(new LambdaQueryWrapper<RxAdvanceRegistrationMiddleSchoolClasses>()
//                .eq(RxAdvanceRegistrationMiddleSchoolClasses::getMiddleSchoolId, updateReq.getId()));
//        //不相等则创建
//        if (!classNum.equals(classesCount)) {
//            //先删除
//            rxAdvanceRegistrationMiddleSchoolClassesMapper.delete(new LambdaQueryWrapper<RxAdvanceRegistrationMiddleSchoolClasses>()
//                    .eq(RxAdvanceRegistrationMiddleSchoolClasses::getMiddleSchoolId, updateReq.getId()));
//            //创建该学校的班级
//            for (int i = 1; i < classNum + 1; i++) {
//                RxAdvanceRegistrationMiddleSchoolClasses rxAdvanceRegistrationMiddleSchoolClasses = new RxAdvanceRegistrationMiddleSchoolClasses();
//                rxAdvanceRegistrationMiddleSchoolClasses.setMiddleSchoolId(updateReq.getId());
//                rxAdvanceRegistrationMiddleSchoolClasses.setMiddleSchoolName(rxAdvanceRegistrationMiddleSchool.getMiddleSchoolName());
//                rxAdvanceRegistrationMiddleSchoolClasses.setMiddleSchoolClassesName(i + "班级");
//                rxAdvanceRegistrationMiddleSchoolClasses.setUserId(userId);
//                rxAdvanceRegistrationMiddleSchoolClasses.setRemark(i);
//                rxAdvanceRegistrationMiddleSchoolClassesMapper.insert(rxAdvanceRegistrationMiddleSchoolClasses);
//            }
//        }

        return CommonResult.success();
    }

    @Override
    public ResponseResult getMiddleSchoolList(Integer pageNum, Integer pageSize, String searchStr) {
        Page<RxAdvanceRegistrationMiddleSchool> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<RxAdvanceRegistrationMiddleSchool> lambdaQueryWrapper = new LambdaQueryWrapper<RxAdvanceRegistrationMiddleSchool>()
                .orderByDesc(RxAdvanceRegistrationMiddleSchool::getCreateTime);
        if (!isNullOrEmpty(searchStr)) {
            lambdaQueryWrapper.like(RxAdvanceRegistrationMiddleSchool::getMiddleSchoolName, searchStr);
        }
        rxAdvanceRegistrationMiddleSchoolMapper.selectPage(page, lambdaQueryWrapper);
        List<RxAdvanceRegistrationMiddleSchoolResp> records = page.getRecords()
                .stream()
                .map(middleSchool -> {
                    RxAdvanceRegistrationMiddleSchoolResp rxAdvanceRegistrationMiddleSchoolResp = new RxAdvanceRegistrationMiddleSchoolResp();
                    BeanUtils.copyProperties(middleSchool, rxAdvanceRegistrationMiddleSchoolResp);
                    return rxAdvanceRegistrationMiddleSchoolResp;
                })
                .collect(Collectors.toList());
        return CommonResult.success(records, (int)page.getTotal());
    }

    @Override
    public ResponseResult updateMiddleSchoolClasses(RxAdvanceRegistrationMiddleSchoolClassesUpdateReq updateReq) {
        if (isNullOrEmpty(updateReq.getId(), updateReq.getMiddleSchoolClassesName())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        RxAdvanceRegistrationMiddleSchoolClasses rxAdvanceRegistrationMiddleSchoolClasses = rxAdvanceRegistrationMiddleSchoolClassesMapper.selectById(updateReq.getId());
        if (isNullOrEmpty(rxAdvanceRegistrationMiddleSchoolClasses)) {
            return CommonResult.failed(CommonCodeEnum.ADVANCE_REGISTRATION_MIDDLE_SCHOOL_CLASSES_NOT_EXIST);
        }
        BeanUtils.copyProperties(updateReq, rxAdvanceRegistrationMiddleSchoolClasses);
        rxAdvanceRegistrationMiddleSchoolClassesMapper.updateById(rxAdvanceRegistrationMiddleSchoolClasses);
        return CommonResult.success();
    }

    @Override
    public ResponseResult deletedMiddleSchoolClasses(RxAdvanceRegistrationMiddleSchoolClassesDeletedReq deletedReq) {
        if (deletedReq.getIdList() == null || deletedReq.getIdList().size() < 1){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        List<RxAdvanceRegistrationMiddleSchoolClasses> middleSchoolClassesList = rxAdvanceRegistrationMiddleSchoolClassesMapper.selectList(
                new LambdaQueryWrapper<RxAdvanceRegistrationMiddleSchoolClasses>()
                .in(RxAdvanceRegistrationMiddleSchoolClasses::getId, deletedReq.getIdList()));
        if (middleSchoolClassesList.isEmpty()) {
            return CommonResult.failed(CommonCodeEnum.ADVANCE_REGISTRATION_MIDDLE_SCHOOL_CLASSES_NOT_EXIST);
        }
        rxAdvanceRegistrationMiddleSchoolClassesMapper.deleteBatchIds(deletedReq.getIdList());
        return CommonResult.success();
    }

    @Override
    public ResponseResult getMiddleSchoolClassesList(Integer pageNum, Integer pageSize, String searchStr, String middleSchoolId) {
        if (isNullOrEmpty(middleSchoolId)) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        Page<RxAdvanceRegistrationMiddleSchoolClasses> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<RxAdvanceRegistrationMiddleSchoolClasses> lambdaQueryWrapper =
                new LambdaQueryWrapper<RxAdvanceRegistrationMiddleSchoolClasses>()
                        .eq(RxAdvanceRegistrationMiddleSchoolClasses::getMiddleSchoolId, middleSchoolId)
                        .orderByDesc(RxAdvanceRegistrationMiddleSchoolClasses::getRemark);
        if (!isNullOrEmpty(searchStr)) {
            lambdaQueryWrapper.like(RxAdvanceRegistrationMiddleSchoolClasses::getMiddleSchoolClassesName, searchStr);
        }
        rxAdvanceRegistrationMiddleSchoolClassesMapper.selectPage(page, lambdaQueryWrapper);
        List<RxAdvanceRegistrationMiddleSchoolClassesResp> rxAdvanceRegistrationMiddleSchoolClassesList = page.getRecords()
                .stream()
                .map(middleSchoolClasses -> {
                    RxAdvanceRegistrationMiddleSchoolClassesResp rxAdvanceRegistrationMiddleSchoolClassesResp = new RxAdvanceRegistrationMiddleSchoolClassesResp();
                    BeanUtils.copyProperties(middleSchoolClasses, rxAdvanceRegistrationMiddleSchoolClassesResp);
                    return rxAdvanceRegistrationMiddleSchoolClassesResp;
                })
                .collect(Collectors.toList());
        return CommonResult.success(rxAdvanceRegistrationMiddleSchoolClassesList,(int)page.getTotal());
    }
}
